package com.thuanpq.breceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.thuanpq.activity.CallPrompt;
import com.thuanpq.global.GlobalCons;
import com.thuanpq.global.GlobalVari;

public class PhoneReceiver extends BroadcastReceiver {

	private boolean rejectBlacklist = false;
	private boolean recordCall = false;
	private String state = "";
	private String number = "";

	@Override
	public void onReceive(Context context, Intent intent) {

		Log.e("ThuanPQ", "PhoneReceiver onReceive()");

		SharedPreferences p = context.getSharedPreferences(
				GlobalCons.SHARED_PREF_NAME, 0);

		rejectBlacklist = p.getBoolean(GlobalCons.SHARED_PREF_REJECT_CALL,
				false);

		recordCall = p.getBoolean(GlobalCons.SHARED_PREF_ANSWER_CALL, false);

		state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

		number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

		GlobalVari.INCOMING_CALL_NUMBER = number;

		if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)
				&& (rejectBlacklist || recordCall)) {

			Log.e("ThuanPQ", "Called from: " + number);

			CallPrompt.launch(context);
		}
	}
}
